// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/testing/gtest_utils.h>
#include <pollux/testing/functions/prestosql/function_base_test.h>
#include <pollux/functions/prestosql/types/json_type.h>

using namespace kumo::pollux;
using namespace kumo::pollux::test;

namespace kumo::pollux::functions {

namespace {

class FailTest : public functions::test::FunctionBaseTest {};

TEST_F(FailTest, basic) {
  auto message = std::optional("test error message");
  POLLUX_ASSERT_USER_THROW(
      evaluateOnce<UnknownValue>("fail(c0)", message), message.value());

  EXPECT_EQ(std::nullopt, evaluateOnce<UnknownValue>("try(fail(c0))", message));

  POLLUX_ASSERT_USER_THROW(
      evaluateOnce<UnknownValue>("fail(123::integer, c0)", message),
      message.value());

  EXPECT_EQ(
      std::nullopt,
      evaluateOnce<UnknownValue>("try(fail(123::integer, c0))", message));
}

TEST_F(FailTest, json) {
  auto json =
      std::optional(R"({"code": 123, "message": "test error message"})");

  POLLUX_ASSERT_USER_THROW(
      evaluateOnce<UnknownValue>("fail(c0)", JSON(), json),
      "test error message");

  EXPECT_EQ(
      std::nullopt, evaluateOnce<UnknownValue>("try(fail(c0))", JSON(), json));

  POLLUX_ASSERT_USER_THROW(
      evaluateOnce<UnknownValue>("fail(123::integer, c0)", JSON(), json),
      "test error message");

  EXPECT_EQ(
      std::nullopt,
      evaluateOnce<UnknownValue>("try(fail(123::integer, c0))", JSON(), json));
}

TEST_F(FailTest, invalidJson) {
  // Invalid JSON.
  POLLUX_ASSERT_USER_THROW(
      evaluateOnce<UnknownValue>(
          "fail(c0)", JSON(), std::optional("not a valid JSON")),
      "Cannot extract 'message' from the JSON");

  // Valid JSON with missing 'message' field.
  POLLUX_ASSERT_USER_THROW(
      evaluateOnce<UnknownValue>("fail(c0)", JSON(), std::optional("{}")),
      "Cannot extract 'message' from the JSON");

  POLLUX_ASSERT_USER_THROW(
      evaluateOnce<UnknownValue>(
          "fail(c0)", JSON(), std::optional(R"({"code": 123})")),
      "Cannot extract 'message' from the JSON");

  // Valid JSON with 'message' field that's not string.
  POLLUX_ASSERT_USER_THROW(
      evaluateOnce<UnknownValue>(
          "fail(c0)",
          JSON(),
          std::optional(R"({"code": 123, "message": [1, 2, 3]})")),
      "Cannot extract 'message' from the JSON");
}

} // namespace
} // namespace kumo::pollux::functions
